迷宫问题(进阶)C/C++

BFS最短路径

利用广度优先搜索来解决迷宫中的最短路径,需要把队列稍微做下调整,博主用的顺序队列,也可以用链式,链式搜索起来方便些。

之前看到有校友用DFS来解决的,但是相对来说算法复杂度要高些,因为DFS一般用来解决所有路径数目问题。
typedef struct{
    MazePos data[10000];
    int front, rear;
    int prior[10000];
}SqQueue;
直接甩代码:
#include <iostream>
#include <malloc.h>
using namespace std;

typedef struct{
    int x, y;
}MazePos;

char maze[104][104];

int x, y;

typedef struct{
    MazePos data[10000];
    int front, rear;
    int prior[10000];
}SqQueue;

void InitQueue(SqQueue &Q)
{
    Q.front = Q.rear = -1;
}

bool EmptyQueue(SqQueue &Q)
{
    if(Q.rear == Q.front)
        return true;
    else return false;
}

void EnQueue(SqQueue &Q, MazePos e)
{
    Q.data[++Q.rear] = e;
    Q.prior[Q.rear] = Q.front;
}

void DeQueue(SqQueue &Q, MazePos &e)
{
    if(!EmptyQueue(Q)){
        e = Q.data[++Q.front];
    }
}

void BFS(SqQueue &Q, MazePos start, MazePos end, int &flag)
{
    MazePos e, k;
    EnQueue(Q, start);
    while(!flag && !EmptyQueue(Q)){
        DeQueue(Q, e);
        if((e.x == end.x) && (e.y == end.y)){
            flag = 1;
            return;
        }
        if((e.x + 1) < x && (maze[e.x + 1][e.y] != '#')){
            k = e;
            ++k.x;
            maze[k.x][k.y] = '#';
            EnQueue(Q, k);
        }
        if((e.y + 1) < y && (maze[e.x][e.y + 1] != '#')){
            k = e;
            ++k.y;
            maze[k.x][k.y] = '#';
            EnQueue(Q, k);
        }
        if((e.x - 1) >= 0 && (maze[e.x - 1][e.y] != '#')){
            k = e;
            --k.x;
            maze[k.x][k.y] = '#';
            EnQueue(Q, k);
        }
        if((e.y - 1) >= 0 && (maze[e.x][e.y - 1] != '#')){
            k = e;
            --k.y;
            maze[k.x][k.y] = '#';
            EnQueue(Q, k);
        }
    }
}

int main()
{
    int i, j, n, flag, count, prim;
    SqQueue q;
    MazePos start, end;
    cin>>n;
    while(n--){
        InitQueue(q);
        flag = 0;
        count = -1;
        cin>>x>>y;
        for(i = 0; i < x; i++){
            for(j = 0; j < y; j++)
                cin>>maze[i][j];
        }
        for(i = 0; i < x; i++)
            for(j = 0; j < y; j++){
                if(maze[i][j] == 'S'){
                    start.x = i;
                    start.y = j;
                }
                if(maze[i][j] == 'E'){
                    end.x = i;
                    end.y = j;
                }
            }
        BFS(q, start, end, flag);
        if(flag){
            prim = q.front;
            while(q.prior[prim] != -1){
                ++count;
                prim = q.prior[prim];
            }
            cout<<count + 1<<endl;
        }
        else cout<<count<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用邻接矩阵存储迷宫的示例代码,以及广度优先搜索算法的实现: ```c++ #include <iostream> #include <queue> #include <vector> using namespace std; // 定义迷宫的邻接矩阵 vector<vector<int>> maze = {{1, 1, 1, 1, 1}, {0, 0, 1, 0, 0}, {1, 1, 1, 0, 1}, {1, 0, 0, 0, 1}, {1, 1, 1, 1, 1}}; // 定义起点和终点的坐标 pair<int, int> start = {1, 0}; pair<int, int> end = {3, 4}; // 定义广度优先搜索算法 bool bfs(vector<vector<int>> maze, pair<int, int> start, pair<int, int> end) { queue<pair<int, int>> q; set<pair<int, int>> visited; q.push(start); while (!q.empty()) { auto node = q.front(); q.pop(); if (node == end) { return true; } if (visited.find(node) == visited.end()) { visited.insert(node); int x = node.first, y = node.second; vector<pair<int, int>> neighbors = {{x-1, y}, {x+1, y}, {x, y-1}, {x, y+1}}; for (auto neighbor : neighbors) { int i = neighbor.first, j = neighbor.second; if (0 <= i && i < maze.size() && 0 <= j && j < maze[0].size() && maze[i][j] == 1) { q.push(neighbor); } } } } return false; } // 测试 int main() { if (bfs(maze, start, end)) { cout << "找到了出口!" << endl; } else { cout << "迷宫没有出口!" << endl; } return 0; } ``` 以上代码中,邻接矩阵 `maze` 存储了迷宫的结构,其中 `1` 表示可以通行的格子,`0` 表示障碍格子。起点和终点的坐标分别为 `start` 和 `end`,使用广度优先搜索算法 `bfs` 在迷宫上寻找从起点到终点的最短路径,如果找到了出口,则返回 true,否则返回 false。在 C++ 中,使用 `vector` 来表示邻接矩阵,使用 `pair` 来表示坐标。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值